package com.dlxj.service.impl;

import java.util.Date;
import java.util.List;

import com.dlxj.dao.BaseDAO;
import com.dlxj.entity.Patroltask;
import com.dlxj.entity.Receiptpatrol;
import com.dlxj.service.ReceiptPatrolService;
import com.dlxj.util.ConstClass;

public class ReceiptPatrolServiceImpl implements ReceiptPatrolService {

	BaseDAO baseDAO;

	@Override
	public int receiptTask(Receiptpatrol receiptpatrol) {
		Receiptpatrol receipt = receiptpatrol;
		receipt.setUid(ConstClass.getCurrentUser().getId());
		receipt.setTime(new Date());
		boolean flag = isCrossPoint(receipt.getPid());// 判断是否是交叉点
		List<Receiptpatrol> list = (List<Receiptpatrol>) baseDAO
				.findByHql(
						"from Receiptpatrol where pid=? and ptid in (select id from Patroltask where state = ?)",
						new Object[] { receipt.getPid(),
								ConstClass.TASK_STATE_EXECUTING });
		if (list.size() > 0) {// 判断是修改还是添加
			Receiptpatrol tmp = list.get(0);
			tmp.setDescription(receipt.getDescription());
			tmp.setBid(receipt.getBid());
			tmp.setIntact(receipt.getIntact());
			tmp.setLevel(receipt.getLevel());
			tmp.setTime(receipt.getTime());
			if (!flag) {// 如果不是交叉点
				tmp.setPtid(receipt.getPtid());
			}
			receipt = tmp;
		}

		Patroltask patroltask = (Patroltask) baseDAO.findById(Patroltask.class,
				receipt.getPtid());
		if (!patroltask.getState().equals(ConstClass.TASK_STATE_EXECUTING)) {// 如果任务状态不是执行中
			return ConstClass.ADD_FAILURE;
		}
		baseDAO.saveOrUpdate(receipt);
		return ConstClass.ADD_SUCCESS;
	}

	public BaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(BaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

	@Override
	public Receiptpatrol findByPidAndPtid(String pid, String ptid) {
		List<Receiptpatrol> list = (List<Receiptpatrol>) baseDAO.findByHql(
				"from Receiptpatrol where pid=? and ptid =?", new Object[] {
						pid, ptid });

		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	private boolean isCrossPoint(String pid) {
		return baseDAO
				.countSql(
						"SELECT COUNT(*) FROM circuitry_pole WHERE pid =? AND pid IN (SELECT pid FROM receiptPatrol WHERE ptid IN (SELECT id FROM patroltask WHERE state=? AND id IN (SELECT pid FROM patroltask_user WHERE uid = ?)))",
						new Object[] { pid, ConstClass.TASK_STATE_EXECUTING,
								ConstClass.getCurrentUser().getId() }) > 1;
	}

	@Override
	public int overTask(String id) {
		Patroltask patroltask = (Patroltask) baseDAO.findById(Patroltask.class,
				id);
		int count = baseDAO.countHql(
				"select count(*) from Receiptpatrol where ptid = ?",
				new Object[] { id });
		if(count<2){
			return 3;
		}	
		patroltask.setState((count > 0 ? ConstClass.TASK_STATE_FINISH
				: ConstClass.TASK_STATE_SURE));
		patroltask.setEndTime(new Date());
		baseDAO.update(patroltask);
		return ConstClass.UPDATE_SUCCESS;
	}

	@Override
	public int receiptTask(String taskNo, String pid, int bid) {
		List<Receiptpatrol> list = (List<Receiptpatrol>) baseDAO.findByHql(
				"from Receiptpatrol where pid = ? and ptid = ? ", new Object[] {
						pid, taskNo });
		if (list.size() > 0) {
			Receiptpatrol receiptpatrol = list.get(0);
			receiptpatrol.setLevel(bid);
			baseDAO.update(receiptpatrol);
			return ConstClass.UPDATE_SUCCESS;
		}
		return ConstClass.UPDATE_FAILURE;
	}

}
